package org.bigjava.novelplatform.common.utils;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author ykh
 */
@Component
public class EnvironmentUtils implements ApplicationContextAware {

    private static Environment env;

    public static Environment getEnvironment() {
        return env;
    }

    public static List<String> getProfile() {
        String[] activeProfiles = getEnvironment().getActiveProfiles();
        return new ArrayList<>(Arrays.asList(activeProfiles));
    }

    public static boolean isProd() {
        List<String> activeProfiles = getProfile();
        boolean isProd = false;
        for (String activeProfile : activeProfiles) {
            if ("prod".equals(activeProfile)) {
                isProd = true;
                break;
            }
        }
        return isProd;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        env = applicationContext.getEnvironment();
    }
}
